package com.globallogic.javatrainings.sergiiivanov.tests;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.util.ArrayList;

import org.junit.Test;

import com.globallogic.javatrainings.sergiiivanov.model.MyArray;
import com.globallogic.javatrainings.sergiiivanov.model.Student;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityFailure;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityWarning;

public class MyArray_use_as_List_Test {

    @Test
    public void testAdd() {
        MyArray<String> arr1 = new MyArray<String>();
        ArrayList<String> arr2 = new ArrayList<String>();
        arr1.add("A");
        arr2.add("A");
        assertArrayEquals(arr1.toArray(), arr2.toArray());
    }

    @Test
    public void testGet() throws UniversityFailure {
        MyArray<String> arr1 = new MyArray<String>();
        ArrayList<String> arr2 = new ArrayList<String>();
        arr1.add("A");
        arr2.add("A");
        assertEquals(arr1.get(0), arr2.get(0));
    }

    @Test
    public void testIndexOf() {
        MyArray<String> arr1 = new MyArray<String>();
        ArrayList<String> arr2 = new ArrayList<String>();
        assertArrayEquals(arr1.toArray(), arr2.toArray());
        arr1.add("A");
        arr1.add("B");
        arr2.add("A");
        arr2.add("B");
        assertEquals(arr1.indexOf("A"), arr2.indexOf("A"));
    }

    @Test
    public void testMyArray() {
        MyArray<String> arr1 = new MyArray<String>();
        ArrayList<String> arr2 = new ArrayList<String>();
        assertEquals(0, arr1.size());
        assertEquals(0, arr2.size());
        assertArrayEquals(arr1.toArray(), arr2.toArray());
    }

    @Test
    public void testMyArrayComplexObjects() throws UniversityWarning {
        MyArray<Student> arr1 = new MyArray<Student>();
        ArrayList<Student> arr2 = new ArrayList<Student>();
        MyArray<Student> arr3 = new MyArray<Student>(new Student[0]);
        assertEquals(0, arr1.size());
        assertEquals(0, arr2.size());
        assertEquals(0, arr3.size());
        assertArrayEquals(arr1.toArray(), arr2.toArray());
        assertArrayEquals(arr2.toArray(), arr3.toArray());
        assertArrayEquals(arr1.toArray(), arr3.toArray());

        Student person1 = new Student("a", "b", "c");
        ArrayList<Student> sarr1 = new ArrayList<Student>();
        assertFalse(sarr1.indexOf(person1) > -1);
        MyArray<Student> sarr2 = new MyArray<Student>();
        assertFalse(sarr2.indexOf(person1) > -1);
    }

    @Test
    public void testRemoveInt() throws UniversityFailure {
        MyArray<String> arr1 = new MyArray<String>();
        ArrayList<String> arr2 = new ArrayList<String>();
        arr1.add("A");
        arr1.add("B");
        arr1.remove(0);
        arr2.add("A");
        arr2.add("B");
        arr2.remove(0);
        assertArrayEquals(arr1.toArray(), arr2.toArray());
    }

    @Test
    public void testRemoveObject() {
        MyArray<String> arr1 = new MyArray<String>();
        ArrayList<String> arr2 = new ArrayList<String>();
        arr1.add("A");
        arr1.add("B");
        arr1.remove("A");
        arr2.add("A");
        arr2.add("B");
        arr2.remove("A");
        assertArrayEquals(arr1.toArray(), arr2.toArray());
    }

    @Test
    public void testSet() throws UniversityFailure {
        MyArray<String> arr1 = new MyArray<String>();
        ArrayList<String> arr2 = new ArrayList<String>();
        arr1.add("A");
        arr2.add("A");
        arr1.set(0, "B");
        arr2.set(0, "B");
        assertArrayEquals(arr1.toArray(), arr2.toArray());
    }

    @Test
    public void testSize() {
        MyArray<String> arr1 = new MyArray<String>();
        ArrayList<String> arr2 = new ArrayList<String>();
        arr1.add("A");
        arr2.add("A");
        assertEquals(arr1.size(), arr2.size());
    }

    @Test
    public void testToArray() {
        MyArray<String> arr1 = new MyArray<String>();
        ArrayList<String> arr2 = new ArrayList<String>();
        assertArrayEquals(arr1.toArray(), arr2.toArray());
        arr1.add("A");
        arr1.add("B");
        arr2.add("A");
        arr2.add("B");
        assertArrayEquals(arr1.toArray(), arr2.toArray());
    }

    @Test
    public void testWipeData() {
        MyArray<String> arr1 = new MyArray<String>();
        ArrayList<String> arr2 = new ArrayList<String>();
        assertArrayEquals(arr1.toArray(), arr2.toArray());
        arr1.add("A");
        arr1.add("B");
        arr1.clear();
        arr2.add("A");
        arr2.add("B");
        arr2.clear();
        assertArrayEquals(arr1.toArray(), arr2.toArray());
    }

}
